const app = getApp();
const { request } = require('../../../utils/request');

Page({
    data: {
        addresses: []
    },

    onShow() {
        this.loadAddresses();
    },

    // 加载地址列表
    async loadAddresses() {
        try {
            // 假设存在获取地址列表的接口，实际按需修改
            const res = await request({
                url: '/address',
                method: 'GET',
                showLoading: true
            });

            if (res.code === 2000) {
              console.log('加载地址成功:', res.data);
                this.setData({ addresses: res.data });
            } else {
                wx.showToast({
                    title: '加载地址失败',
                    icon: 'none'
                });
                console.error('加载地址失败:', res.message);
            }
        } catch (error) {
            wx.showToast({
                title: '请求出错',
                icon: 'none'
            });
            console.error('加载地址请求出错:', error);
        }
    },

    // 编辑地址
    editAddress(e) {
        const id = e.currentTarget.dataset.id;
        const address = this.data.addresses.find(item => item.id === id);
        wx.navigateTo({
            url: `/page/component/address/editAddress?address=${encodeURIComponent(JSON.stringify(address))}`
        });
    },

    // 删除地址
    async deleteAddress(e) {
        const id = e.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要删除该地址吗？',
            success: async (res) => {
                if (res.confirm) {
                    try {
                        // 调用删除地址接口
                        const delRes = await request({
                            url: `/address/${id}`,
                            method: 'DELETE',
                            showLoading: true
                        });

                        if (delRes.code === 2000) {
                            this.loadAddresses();
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success'
                            });
                        } else {
                            wx.showToast({
                                title: '删除失败',
                                icon: 'none'
                            });
                            console.error('删除地址失败:', delRes.message);
                        }
                    } catch (error) {
                        wx.showToast({
                            title: '请求出错',
                            icon: 'none'
                        });
                        console.error('删除地址请求出错:', error);
                    }
                }
            }
        });
    },

    // 跳转到新增地址页面
    navigateToAddAddress() {
        wx.navigateTo({
            url: '/page/component/addAddress/index'
        });
    },

    // 设置默认地址
    async setDefaultAddress(e) {
        const id = e.currentTarget.dataset.id;
        try {
            const res = await request({
                url: `/address/${id}/setDefault`,
                method: 'PUT',
                showLoading: true
            });

            if (res.code === 2000) {
                this.loadAddresses();
                wx.showToast({
                    title: '设置默认成功',
                    icon: 'success'
                });
            } else {
                wx.showToast({
                    title: '设置默认失败',
                    icon: 'none'
                });
                console.error('设置默认地址失败:', res.message);
            }
        } catch (error) {
            wx.showToast({
                title: '请求出错',
                icon: 'none'
            });
            console.error('设置默认地址请求出错:', error);
        }
    }
});